<script setup>
import {cloneDeep} from "lodash-es";
import SettingPermissionAPI from "@/api/setting/permission.js";
import {arr2Tree, objMerge} from "@/utils/tools.js";
import SettingMenuAPI from "@/api/setting/menu.js";
import {convertMenuToTree} from "@/views/setting/menu/common.js";
import SettingRoleAPI from "@/api/setting/role.js";

defineProps({
  cascaderData: {
    type: Array,
  }
})

const emits = defineEmits(["close"]);
const priId = ref(0)
const open = (id) => {
  if (id) {
    dialog.title = '编辑'
    priId.value = id
    SettingRoleAPI.getFormData(id).then(res => {
      objMerge(formData, res, ['menus'])
      formData.menus = res.menus.split(',')
    })
  } else {
    dialog.title = '新增'
    priId.value = 0
  }
  dialog.visible = true;
  getTreeData()
}
defineExpose({open});

const dialog = reactive({
  visible: false,
  title: "新增",
});

const formRef = ref();
let formData = reactive({
  name: "",
  key: "",
  remark: "",
  menus: [],
});

const rules = reactive({
  name: [{required: true, message: "不能为空", trigger: "blur"}],
  key: [{required: true, message: "不能为空", trigger: "blur"}],
  menus: [{required: true, message: "请选择", trigger: "blur"}],
});


const __DialogClose = () => {
  dialog.visible = false;
  formRef.value.resetFields();
  formRef.value.clearValidate();
  emits("close");
};

const _submit = (formEl) => {
  if (!formEl) return
  formData.menus = treeRef.value.getCheckedKeys(true);
  const data = cloneDeep(formData)
  formEl.validate((valid, fields) => {
    if (valid) {
      data.relMenus = [...treeRef.value.getCheckedKeys(), ...treeRef.value.getHalfCheckedKeys()]
      data.menus = data.menus.join(',')
      if (priId.value > 0) {
        SettingRoleAPI.update(priId.value, data).then(res => {
          __DialogClose()
        })
      } else {
        SettingRoleAPI.add(data).then(res => {
          __DialogClose()
        })
      }
    } else {
      console.log('error submit!', fields)
    }
  })
}

const treeRef = ref()
const treeData = ref([])
const getTreeData = () => {
  SettingMenuAPI.getList().then((res) => {
    treeData.value = arr2Tree(res)
  })
}
</script>

<template>
  <el-dialog
      v-model="dialog.visible"
      :title="dialog.title"
      append-to-body
      destroy-on-close
      width="30vw"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      :before-close="__DialogClose"
      draggable
  >
    <el-form ref="formRef" label-width="120px" :model="formData" :rules="rules">
      <el-form-item label="角色" prop="name">
        <el-input v-model="formData.name" maxlength="20" placeholder="名称"/>
      </el-form-item>
      <el-form-item label="KEY" prop="key">
        <el-input v-model="formData.key" maxlength="20" placeholder="key"/>
      </el-form-item>
      <el-form-item label="菜单权限" prop="menus">
        <el-tree
            ref="treeRef"
            :data="treeData"
            show-checkbox
            default-expand-all
            :default-checked-keys="formData.menus"
            node-key="id"
            :props="{label: 'title'}"
        />
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input v-model="formData.remark" type="textarea" :rows="4" placeholder="备注"/>
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="__DialogClose">取 消</el-button>
        <el-button type="primary" @click="_submit(formRef)">确 定</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<style lang="scss" scoped>

</style>
